<template>
    <el-dialog @close="beforeClose" :close-on-click-modal="true" width="20%" :modal="false" :center="true" :visible.sync='showRemoteDialog'>
        <div>
          输入客户ID码建立连接
        </div>
        <el-input v-model="remoteCode"></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click.stop="confirm" class="primary_btn">确定</el-button>
      </div>
    </el-dialog>
</template>

<script>
    export default {
      name: 'remoteControlInput',
      data () {
        return {
          remoteCode: '',
          showRemoteDialog: true,
          url: '',
          error: ''
        }
      },
      computed: {
        RemoteControlInfo () {
          return this.$store.state.RemoteControl
        }
      },
      watch: {
        remoteCode () {
          console.log(this.remoteCode.length)
          if (this.remoteCode !== '' && this.remoteCode.length === 6) {
            this.checkCode()
          } else if (this.remoteCode !== '' && this.remoteCode.length > 6) {
            this.$store.commit('SET_ERROR', 'remoteControl.code_bit_is_error')
          }
        }
      },
      methods: {
        checkCode () {
          let data = {
            code: this.remoteCode,
            source: this.RemoteControlInfo.remoteFrom,
            customerId: this.RemoteControlInfo.remoteCustSid
          }
          this.$store.dispatch('queryRemoteControlAdd', data).then(req => {
            if (req.success && req.data) {
              let sessionId = window.sessionStorage.getItem('sessionId')
              this.url = `http://rtc.7moor.com:6688/?address=${req.data.address}&cid=${req.data.session}&connectCode=${this.remoteCode}&sessionId=${sessionId}`
              this.error = ''
              console.log(this.url)
            } else if (!req.success && req.message) {
              this.error = req.message
              console.log(this.error)
            }
          })
        },
        confirm () {
          if (this.remoteCode === '') {
            this.$message.error(this.$t('remoteControl.codeRequired'))
            return
          }
          if (this.url) {
            window.open(this.url)
            let data = {
              isRemoteControl: false,
              remoteFrom: '',
              remoteCustSid: ''
            }
            this.$store.commit('updateRemoteControlInfo', data)
          }
          if (this.error && !this.url) {
            this.$store.commit('SET_ERROR', this.error)
          }
          if (!this.error && !this.url) {
            this.$store.commit('SET_ERROR', 'remoteControl.getConnectInfoError')
          }
        },
        beforeClose () {
          let data = {
            isRemoteControl: false,
            remoteFrom: '',
            remoteCustSid: ''
          }
          this.$store.commit('updateRemoteControlInfo', data)
        }
      }
    }
</script>

<style scoped>

</style>
